//
//  Constant.h
//  VET
//
//  Created by Lam BG on 2018/6/26.
//  Copyright © 2018年 YZ. All rights reserved.
//

#pragma mark -- 系统 型号
#define kSystemVersionAsFloat  ([UIDevice currentDevice].systemVersion.floatValue)
#define kSystemVersion  ([UIDevice currentDevice].systemVersion)

#define kIsiOS7         ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7)
#define kIsiOS8         ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8)
#define iOS8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define iOS9_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
#define iOS10_OR_LATER ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0)

//宏定义，判断是否是 iOS10.0以上
#define kIsiPhone4        ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define kIsiPhone5        ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define kIsiPhoneScreenSmallThan6   ([[UIScreen mainScreen] bounds].size.height <= 667.0f)
#define kIsiPhoneSmall   ([[UIScreen mainScreen] bounds].size.height < 667.0f)
#define kIsiPhone6       ([[UIScreen mainScreen] bounds].size.height == 667.0f)
#define kIsiPhonePlus    ([[UIScreen mainScreen] bounds].size.height == 736.0f)
#define kIsiPhoneX       ([[UIScreen mainScreen] bounds].size.height == 812.0f)

#pragma mark -- 版本
// app 版本
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// build
#define kAppBuildVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

#pragma mark -

#pragma mark -- 屏幕宽高

#define kScreenWidth   [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight  [[UIScreen mainScreen] bounds].size.height
#define kScreenBounds [[UIScreen mainScreen] bounds];

#pragma mark -- app 状态栏和导航栏高度

#define kRectNavAndStatusHight  (self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height)

#pragma mark -

//检查空
#define kNullable(value) (value == [NSNull null] || value == nil ? nil : value)
#define kText(value) (value == nil ? @"" : value)
#define kTextDefault(value, defalut) (value == nil ? defalut : value)

#pragma mark -- 颜色 functions

// RGBA 创建颜色
#define kColorWithRGB(x,y,z) [UIColor colorWithRed:(float)x/255.0 green:(float)y/255.0 blue:(float)z/255.0 alpha:1]
#define kColorWithRGBA(x,y,z,a) [UIColor colorWithRed:(float)x/255.0 green:(float)y/255.0 blue:(float)z/255.0 alpha:(float)a]

// 十六进制创建颜色
#define kColorWithHex(hexValue) [UIColor \
colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 \
green:((float)((hexValue & 0xFF00) >> 8))/255.0 \
blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]

#define kColorWithHexAlpha(hexValue,a) [UIColor \
colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 \
green:((float)((hexValue & 0xFF00) >> 8))/255.0 \
blue:((float)(hexValue & 0xFF))/255.0 alpha:(a)]


#define YZ_IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define YZ_IS_PAD (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)

// 等比例适配系数
#define kScaleFit (YZ_IS_IPHONE ? ((kScreenWidth < kScreenHeight) ? kScreenWidth / 375.0f : kScreenWidth / 667.0f) : 1.1f)

#define kCurrentDeviceNum(X) X*kScaleFit
#define kCurrentDeviceFont(X) [UIFont systemFontOfSize:X*kScaleFit]
#define kCurrentDeviceBoldFont(X) [UIFont boldSystemFontOfSize:X*kScaleFit]


#define kPickerHeight 197
#define kTopViewHeight 44

// 状态栏的高度(20 / 44(iPhoneX))
#define YZ_STATUSBAR_HEIGHT ([UIApplication sharedApplication].statusBarFrame.size.height)
#define YZ_IS_iPhoneX ((YZ_STATUSBAR_HEIGHT == 44) ? YES : NO)
#define kStatusBarHeight      (YZ_IS_iPhoneX ? 44.f : 20.f)
// 底部安全区域远离高度
#define YZ_BOTTOM_MARGIN (CGFloat)(YZ_IS_iPhoneX ? 34 : 0)

// 默认主题颜色
#define kDefaultThemeColor kColorWithHex(0x464646)
// topView视图的背景颜色
#define kYZToolBarColor kColorWithHex(0xFDFDFD)

#pragma mark -  强，弱引用宏

#define YZWeakSelf(type)  __weak typeof(type) weak##type = type;
#define YZStrongSelf(type)  __strong typeof(type) strong##type = weak##type;

#pragma mark -- 时间间隔
#define TimeStart		CFAbsoluteTime startTime;	startTime=CFAbsoluteTimeGetCurrent();
#define TimeEnd			NSLog(@"[%s] during:  %@ %f seconds", class_getName([self class]), NSStringFromSelector(_cmd), CFAbsoluteTimeGetCurrent() - startTime );
#pragma mark -

#pragma mark -- 打印引用计数
#define LogRetainCount(obj) NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)obj));
#pragma mark -

#pragma mark -- 单例
#define SingletonH + (instancetype)sharedInstance;
#define SingletonM \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)sharedInstance \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}\
\
- (id)mutableCopyWithZone:(NSZone *)zone { \
return _instance; \
}
#pragma mark -

#pragma mark -- user default

// 保存对象到本地
#define UserDefaultsAsyncSavaObjToLocal(key, value) \
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ \
NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:value]; \
[[NSUserDefaults standardUserDefaults] setObject:userData forKey:key]; \
[[NSUserDefaults standardUserDefaults] synchronize]; \
});

// 读取本地对象
#define UserDefaultsGetObjFromLocal(key) \
({\
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key]; \
id obj = [NSKeyedUnarchiver unarchiveObjectWithData:data]; \
(obj); \
})

// 清空本地保存对象
#define UserDefaultsClearObjFromLocal(key) \
[[NSUserDefaults standardUserDefaults] removeObjectForKey: key]; \
[[NSUserDefaults standardUserDefaults] synchronize];






